package com.mybatis.boost.generator.plugin;

import com.mybatis.boost.generator.plugin.adapter.MapperPluginAdapter;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.api.dom.xml.Document;
import org.mybatis.generator.config.Context;

import java.util.List;
import java.util.Properties;

/**
 * 配合通用Mapper的生成器插件
 *
 * @author jearton
 * @since 2017/2/4
 */
@Slf4j
public class MapperStarterPlugin extends MapperPluginAdapter {

    private MapperPlugin mapperPlugin = new MapperPlugin();

    private Jsr310Plugin jsr310Plugin = new Jsr310Plugin();
    private SerializablePlugin serializablePlugin = new SerializablePlugin();
    private FluentSetterPlugin fluentSetterPlugin = new FluentSetterPlugin();
    private LombokPlugin lombokPlugin = new LombokPlugin();

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public void setContext(Context context) {
        mapperPlugin.setContext(context);
        jsr310Plugin.setContext(context);
        serializablePlugin.setContext(context);
        fluentSetterPlugin.setContext(context);
        lombokPlugin.setContext(context);
    }

    @Override
    public void setProperties(Properties properties) {
        mapperPlugin.setProperties(properties);
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        jsr310Plugin.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
        mapperPlugin.modelFieldGenerated(field, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
        return true;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        return lombokPlugin.modelGetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method, TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn, IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        fluentSetterPlugin.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
        return lombokPlugin.modelSetterMethodGenerated(method, topLevelClass, introspectedColumn, introspectedTable, modelClassType);
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        mapperPlugin.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
        serializablePlugin.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
        lombokPlugin.modelBaseRecordClassGenerated(topLevelClass, introspectedTable);
        return true;
    }

    @Override
    public boolean modelPrimaryKeyClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        mapperPlugin.modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
        serializablePlugin.modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
        lombokPlugin.modelPrimaryKeyClassGenerated(topLevelClass, introspectedTable);
        return true;
    }

    @Override
    public boolean modelRecordWithBLOBsClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        mapperPlugin.modelRecordWithBLOBsClassGenerated(topLevelClass, introspectedTable);
        serializablePlugin.modelRecordWithBLOBsClassGenerated(topLevelClass, introspectedTable);
        lombokPlugin.modelRecordWithBLOBsClassGenerated(topLevelClass, introspectedTable);
        return true;
    }

    @Override
    public boolean clientGenerated(Interface interfaze, TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        return mapperPlugin.clientGenerated(interfaze, topLevelClass, introspectedTable);
    }

    @Override
    public boolean sqlMapDocumentGenerated(Document document, IntrospectedTable introspectedTable) {
        return mapperPlugin.sqlMapDocumentGenerated(document, introspectedTable);
    }
}
